# -*- coding: utf-8 -*-
# @Time : 2021/7/4 22:10
# @Author : liuha
# @File : page_ecshop_back_member.py
# @Project : ECshopGitee
import time

from selenium.webdriver.common.by import By


class PageEcshopBackMember():
    ''' 后台商品添加 '''

    def __init__(self, driver):
        self.driver = driver
        # 元素定位器
        self.loc_member_keyword = (By.NAME, "keyword")  # 搜索框
        self.loc_member_search_btn = (By.XPATH, "/html/body/div[1]/form/input[4]")  # 搜索按钮
        self.loc_member_del_btn = (By.XPATH, '//div[@id="listDiv"]/table/tbody/tr[3]/td[10]/a[5]/img')  # 删除按钮
        self.loc_member_add_btn = (By.LINK_TEXT, '添加会员')  # 添加用户
        self.loc_member_search_assert = (By.XPATH, '//div[@id="listDiv"]/table/tbody/tr[3]/td[2]')  # 搜索的断言

        self.loc_member_username = (By.NAME, "username")  # 用户名
        self.loc_member_email = (By.NAME, "email")  # 邮箱
        self.loc_member_password = (By.NAME, "password")  # 密码
        self.loc_member_confirm_password = (By.NAME, "confirm_password")  # 确认密码
        self.loc_member_user_rank = (By.NAME, "user_rank")  # 用户等级下拉框
        self.loc_member_credit_line = (By.NAME, "credit_line")  # 信用额度

        self.loc_member_sex_secrecy = (By.XPATH, "/html/body/div[1]/form/table/tbody/tr[6]/td[2]/input[1]")  # 性别保密
        self.loc_member_sex_man = (By.XPATH, "/html/body/div[1]/form/table/tbody/tr[6]/td[2]/input[2]")  # 性别男
        self.loc_member_sex_woman = (By.XPATH, "/html/body/div[1]/form/table/tbody/tr[6]/td[2]/input[3]")  # 性别女

        self.loc_member_year = (By.NAME, "/html/body/div[1]/form/table/tbody/tr[6]/td[2]/input[3]")  # 出生日期年
        self.loc_member_month = (By.NAME, "/html/body/div[1]/form/table/tbody/tr[6]/td[2]/input[3]")  # 出生日期年
        self.loc_member_day = (By.NAME, "/html/body/div[1]/form/table/tbody/tr[6]/td[2]/input[3]")  # 出生日期年

        self.loc_member_qq = (By.NAME, "extend_field2")  # qq
        self.loc_member_work_phone = (By.NAME, "extend_field3")  # 办公电话
        self.loc_member_home_phone = (By.NAME, "extend_field4")  # 家庭电话
        self.loc_member_phone = (By.NAME, "extend_field5")  # 手机电话
        self.loc_member_submit = (By.XPATH, "//input[@value=' 确定 ']")  # 提交按钮

    def member_keyword(self, keyword):  # 输入搜索内容
        self.driver.find_element(*self.loc_member_keyword).send_keys(keyword)

    def member_search_assert(self):  # 搜索的第一个结果
        act = self.driver.find_element(*self.loc_member_search_assert).text
        return act

    def member_search_btn(self):  # 搜索按钮点击
        self.driver.find_element(*self.loc_member_search_btn).click()

    def member_del_btn(self):  # 点击删除按钮
        self.driver.find_element(*self.loc_member_del_btn).click()

    def member_add_btn(self):  # 点击添加用户
        self.driver.find_element(*self.loc_member_add_btn).click()

    def member_username(self, username):  # 输入用户名
        self.driver.find_element(*self.loc_member_username).send_keys(username)

    def member_email(self, email):  # 输入邮箱
        self.driver.find_element(*self.loc_member_email).send_keys(email)

    def member_password(self, password):  # 输入密码
        self.driver.find_element(*self.loc_member_password).send_keys(password)

    def member_confirm_password(self, confirm_password):  # 确认密码
        self.driver.find_element(*self.loc_member_confirm_password).send_keys(confirm_password)

    def member_phone(self, phone):  # 输入手机号
        self.driver.find_element(*self.loc_member_phone).send_keys(phone)

    def member_submit(self):  # 点击提交
        self.driver.find_element(*self.loc_member_submit).click()

    def member_search(self, keyword):
        ''' 搜索用户 '''
        self.member_keyword(keyword)
        self.member_search_btn()
        act = self.member_search_assert()
        return act

    def member_del(self):
        ''' 删除用户 '''
        self.member_del_btn()
        time.sleep(1)
        self.driver.switch_to.alert.accept()

    def member_add(self, username, email, password, confirm_password, phone):
        ''' 用户添加 '''
        self.member_add_btn()
        self.member_username(username)
        self.member_email(email)
        self.member_password(password)
        self.member_confirm_password(confirm_password)
        self.member_phone(phone)
        self.member_submit()
